package com.jilefojing.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.jilefojing.beans.enums.GenderEnum;
import com.jilefojing.beans.pojos.WriteBook;
import com.jilefojing.beans.query.WriteBookQuery;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.dao.IWriteBookDao;
import com.jilefojing.dao.base.impl.BaseDao;
import com.jilefojing.dao.mapper.ITWriteBookMapper;
import com.jilefojing.util.Detect;

/**
 * 
 * @author 作者 jgshun
 * @date 创建时间 2016年10月07日 00时08分14秒
 */
@Repository
public class WriteBookDao extends BaseDao<WriteBook> implements IWriteBookDao {
	@Autowired
	private ITWriteBookMapper tWriteBookMapper;

	@Override
	public Pagination<WriteBook> getPagination(WriteBookQuery writeBookQuery,
			Pagination<WriteBook> pagination) {

		Example example = new Example(WriteBook.class);
		Criteria criteria = example.createCriteria();
		if (Detect.notEmpty(writeBookQuery.getIds())) {
			criteria.andIn(WriteBook.FIELD_ID, writeBookQuery.getIds());
		}

		if (Detect.notEmpty(writeBookQuery.getLoginUserIds())) {
			criteria.andIn(WriteBook.FIELD_LOGINUSER,
					writeBookQuery.getLoginUserIds());
		}

		if (Detect.notEmpty(writeBookQuery.getBuddhismIds())) {
			criteria.andIn(WriteBook.FIELD_BUDDHISM,
					writeBookQuery.getBuddhismIds());
		}

		if (Detect.notEmpty(writeBookQuery.getBookChapterIds())) {
			criteria.andIn(WriteBook.FIELD_BOOKCHAPTERID,
					writeBookQuery.getBookChapterIds());
		}

		if (Detect.notEmpty(writeBookQuery.getTelephone())) {
			criteria.andEqualTo(WriteBook.FIELD_TELEPHONE,
					writeBookQuery.getTelephone());
		}

		if (Detect.notEmpty(writeBookQuery.getAddress())) {
			criteria.andEqualTo(WriteBook.FIELD_ADDRESS,
					writeBookQuery.getAddress());
		}

		if (Detect.notEmpty(writeBookQuery.getLike_address())) {
			criteria.andLike(WriteBook.FIELD_ADDRESS,
					"%" + writeBookQuery.getLike_address() + "%");
		}

		if (Detect.notEmpty(writeBookQuery.getWriteUserName())) {
			criteria.andEqualTo(WriteBook.FIELD_WRITEUSERNAME,
					writeBookQuery.getWriteUserName());
		}

		if (Detect.notEmpty(writeBookQuery.getLike_writeUserName())) {
			criteria.andLike(WriteBook.FIELD_WRITEUSERNAME, "%"
					+ writeBookQuery.getLike_writeUserName() + "%");
		}
		if (Detect.notEmpty(writeBookQuery.getGenders())) {
			List<Short> genderCodes = new ArrayList<Short>();
			for (GenderEnum _Gender : writeBookQuery.getGenders()) {
				genderCodes.add(_Gender.getCode());
			}
			criteria.andIn(WriteBook.FIELD_GENDER, genderCodes);
		}

		if (writeBookQuery.getEqualAge() > 0) {
			criteria.andEqualTo(WriteBook.FIELD_AGE,
					writeBookQuery.getEqualAge());
		}

		if (writeBookQuery.getEqualAndBeforAge() > 0) {
			criteria.andGreaterThanOrEqualTo(WriteBook.FIELD_AGE,
					writeBookQuery.getEqualAndBeforAge());
		}

		if (writeBookQuery.getEqualAndAfterAge() > 0) {
			criteria.andLessThanOrEqualTo(WriteBook.FIELD_AGE,
					writeBookQuery.getEqualAndAfterAge());
		}

		if (writeBookQuery.getBeforAge() > 0) {
			criteria.andGreaterThan(WriteBook.FIELD_AGE,
					writeBookQuery.getBeforAge());
		}

		if (writeBookQuery.getAfterAge() > 0) {
			criteria.andLessThan(WriteBook.FIELD_AGE,
					writeBookQuery.getAfterAge());
		}

		if (writeBookQuery.getEqualCreateTime() != null) {
			criteria.andEqualTo(WriteBook.FIELD_CREATETIME,
					writeBookQuery.getEqualCreateTime());
		}

		if (writeBookQuery.getEqualAndBeforCreateTime() != null) {
			criteria.andGreaterThanOrEqualTo(WriteBook.FIELD_CREATETIME,
					writeBookQuery.getEqualAndBeforCreateTime());
		}

		if (writeBookQuery.getEqualAndAfterCreateTime() != null) {
			criteria.andLessThanOrEqualTo(WriteBook.FIELD_CREATETIME,
					writeBookQuery.getEqualAndAfterCreateTime());
		}

		if (writeBookQuery.getBeforCreateTime() != null) {
			criteria.andGreaterThan(WriteBook.FIELD_CREATETIME,
					writeBookQuery.getBeforCreateTime());
		}

		if (writeBookQuery.getAfterCreateTime() != null) {
			criteria.andLessThan(WriteBook.FIELD_CREATETIME,
					writeBookQuery.getAfterCreateTime());
		}

		if (Detect.notEmpty(pagination.getOrders())) {
			example.setOrderByClause(pagination.getOrdersToStr());
		}

		return super.getPagination(pagination, example);
	}
}
